<?php
namespace Admin\Controller;
class RoleController extends BaceController{
 /* 
  *Role角色管理
  */        
    function role_add (){  
        //添加角色
       if (!empty($_POST)) {  
           $roleOb=M('Role');
           $time=date("Y-m-d H:i:s",time());
           $data['updated_time']=$time;
           $data['name']=$_POST['name'];
          if( mb_strlen($data) <1 || mb_strlen($data) >15 ) {
              $this->ajaxReturn(array('pass'=>1,'msg'=>'请输入合法角色名！'));
          }
           if( $roleOb->add($data) ) {
               $this->ajaxReturn(array('pass'=>1,'msg'=>'添加成功'));
           }
           
          

       }
       $this->display();
    }
    function role_list () {                  //分页
            //1.实例化数据模型类
        $roleOb=M('Role');
        //2.获取总记录数
        $num=$roleOb->count();
        //3.指定每夜显示的条数
        $pageSize=5;
        //4.实例化分页类
        $page=new\Think\Page($num,$pageSize);
        $page->setConfig('prev','上一页');
        $page->setConfig('next','下一页');
        //5.调用show获取分页的结果
        $pageSrt=$page->show();
        //6.获取limit子句的开始位置
        $start=$page->firstRow;
        //7.把分页结果传到模版上
        $this->assign('pageSrt',$pageSrt);
        //8.获取当前页的记录，传到模版
        $arr=$roleOb->order('id desc')
                    ->limit($start,$pageSize)
                    ->select();
        $this->assign('arr',$arr);
        //9.显示模版
       
        $this->display();
       
    }
        function role_save(){      //点击编辑角色
            $id=$_GET['id'];
            $name=$_GET['name'];
           $this->assign("id",$id);
            $this->assign('name',$name);

            if (!empty($_POST)){
                
               $Role=M('Role');
               $re=$Role->create();
               
               if ($re){
                  $name=$_POST['name'];
                  $id=$_POST['id'];
                   $cRe=$Role->where("id=$id")->save();
                   $info=array('pass'=>1,'msg'=>"编辑成功");
                   $this->ajaxReturn($info);
               }
            }
           $this->display();
        }
       function delete(){    //点击删除
           $where['id']=array('in',$_POST['id']);
           $RoleOb=M('Role');
            $re=$RoleOb->where($where)->delete();
           if ($re){
               $data['a']='pass';
           };
           $this->ajaxReturn($data);
       }
       
       function role_access() {
        //保存角色权限关系表
         if (!empty($_POST)) { //
           $access_ids= $_POST['access_id'];
            foreach ($access_ids as $v){
                 $access_id=$v;
                 $role_id = $_POST['role_id'];
                 $data=array(
                     'access_id'=>$access_id,
                     'role_id'=>$role_id
                 );
                 $re=M('Role_access')->add($data);
            }
            var_dump($re);
            /* if ($re) {
                $this->ajaxReturn(array('pass'=>1,'msg'=>'操作成功!'));
             } */
         }else {
             //查询所有的权限
        
             $accessOb=M('Access')->where('status=1')->select();
             
             $this->assign('accessOb',$accessOb);
             //get传过来的角色id角色名
             $id=$_GET['id'];
             $this->assign('id',$id);
             $name=$_GET['name'];
             $this->assign('name',$name);
             //查询所有以分配的权限
             $role_access=M('Role_access')->where("role_id=$id")->field("access_id")->select();
                foreach ($role_access as $k=>$v) {
                    $role_access_id[$k]=$v;
                }
                 /* foreach ($role_access_id as $v){
                    $access_id=$v['access_id'];
                 
                     
                    var_dump($access_id);
                    $this->assign('access_id',$access_id);
                    
                }  */
                $this->assign('role_access_id',$role_access_id);
               
            
         }

           $this->display();
       }
       
}